using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Adex
{
	/// <summary>
	/// Strongly-typed collection for the Program class.
	/// </summary>
	[Serializable]
	public partial class ProgramCollection : ActiveList<Program, ProgramCollection> 
	{	   
		public ProgramCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Program table.
	/// </summary>
	[Serializable]
	public partial class Program : ActiveRecord<Program>
	{
		#region .ctors and Default Settings
		
		public Program()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Program(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Program(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Program(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Program", TableType.Table, DataService.GetInstance("Adex"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarProgramID = new TableSchema.TableColumn(schema);
				colvarProgramID.ColumnName = "ProgramID";
				colvarProgramID.DataType = DbType.Int32;
				colvarProgramID.MaxLength = 0;
				colvarProgramID.AutoIncrement = true;
				colvarProgramID.IsNullable = false;
				colvarProgramID.IsPrimaryKey = false;
				colvarProgramID.IsForeignKey = false;
				colvarProgramID.IsReadOnly = false;
				colvarProgramID.DefaultSetting = @"";
				colvarProgramID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarProgramID);
				
				TableSchema.TableColumn colvarProgramCode = new TableSchema.TableColumn(schema);
				colvarProgramCode.ColumnName = "ProgramCode";
				colvarProgramCode.DataType = DbType.Int32;
				colvarProgramCode.MaxLength = 0;
				colvarProgramCode.AutoIncrement = false;
				colvarProgramCode.IsNullable = false;
				colvarProgramCode.IsPrimaryKey = true;
				colvarProgramCode.IsForeignKey = false;
				colvarProgramCode.IsReadOnly = false;
				colvarProgramCode.DefaultSetting = @"";
				colvarProgramCode.ForeignKeyTableName = "";
				schema.Columns.Add(colvarProgramCode);
				
				TableSchema.TableColumn colvarProgramName = new TableSchema.TableColumn(schema);
				colvarProgramName.ColumnName = "ProgramName";
				colvarProgramName.DataType = DbType.String;
				colvarProgramName.MaxLength = 255;
				colvarProgramName.AutoIncrement = false;
				colvarProgramName.IsNullable = true;
				colvarProgramName.IsPrimaryKey = false;
				colvarProgramName.IsForeignKey = false;
				colvarProgramName.IsReadOnly = false;
				colvarProgramName.DefaultSetting = @"";
				colvarProgramName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarProgramName);
				
				TableSchema.TableColumn colvarMediaCode = new TableSchema.TableColumn(schema);
				colvarMediaCode.ColumnName = "MediaCode";
				colvarMediaCode.DataType = DbType.Int32;
				colvarMediaCode.MaxLength = 0;
				colvarMediaCode.AutoIncrement = false;
				colvarMediaCode.IsNullable = true;
				colvarMediaCode.IsPrimaryKey = false;
				colvarMediaCode.IsForeignKey = true;
				colvarMediaCode.IsReadOnly = false;
				colvarMediaCode.DefaultSetting = @"";
				
					colvarMediaCode.ForeignKeyTableName = "Medium";
				schema.Columns.Add(colvarMediaCode);
				
				TableSchema.TableColumn colvarTv = new TableSchema.TableColumn(schema);
				colvarTv.ColumnName = "TV";
				colvarTv.DataType = DbType.Boolean;
				colvarTv.MaxLength = 0;
				colvarTv.AutoIncrement = false;
				colvarTv.IsNullable = false;
				colvarTv.IsPrimaryKey = false;
				colvarTv.IsForeignKey = false;
				colvarTv.IsReadOnly = false;
				
						colvarTv.DefaultSetting = @"((0))";
				colvarTv.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTv);
				
				TableSchema.TableColumn colvarRd = new TableSchema.TableColumn(schema);
				colvarRd.ColumnName = "RD";
				colvarRd.DataType = DbType.Boolean;
				colvarRd.MaxLength = 0;
				colvarRd.AutoIncrement = false;
				colvarRd.IsNullable = false;
				colvarRd.IsPrimaryKey = false;
				colvarRd.IsForeignKey = false;
				colvarRd.IsReadOnly = false;
				
						colvarRd.DefaultSetting = @"((0))";
				colvarRd.ForeignKeyTableName = "";
				schema.Columns.Add(colvarRd);
				
				TableSchema.TableColumn colvarCb = new TableSchema.TableColumn(schema);
				colvarCb.ColumnName = "CB";
				colvarCb.DataType = DbType.Boolean;
				colvarCb.MaxLength = 0;
				colvarCb.AutoIncrement = false;
				colvarCb.IsNullable = false;
				colvarCb.IsPrimaryKey = false;
				colvarCb.IsForeignKey = false;
				colvarCb.IsReadOnly = false;
				
						colvarCb.DefaultSetting = @"((0))";
				colvarCb.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCb);
				
				TableSchema.TableColumn colvarDateAdded = new TableSchema.TableColumn(schema);
				colvarDateAdded.ColumnName = "DateAdded";
				colvarDateAdded.DataType = DbType.DateTime;
				colvarDateAdded.MaxLength = 0;
				colvarDateAdded.AutoIncrement = false;
				colvarDateAdded.IsNullable = false;
				colvarDateAdded.IsPrimaryKey = false;
				colvarDateAdded.IsForeignKey = false;
				colvarDateAdded.IsReadOnly = false;
				
						colvarDateAdded.DefaultSetting = @"(getdate())";
				colvarDateAdded.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDateAdded);
				
				TableSchema.TableColumn colvarDateModified = new TableSchema.TableColumn(schema);
				colvarDateModified.ColumnName = "DateModified";
				colvarDateModified.DataType = DbType.DateTime;
				colvarDateModified.MaxLength = 0;
				colvarDateModified.AutoIncrement = false;
				colvarDateModified.IsNullable = false;
				colvarDateModified.IsPrimaryKey = false;
				colvarDateModified.IsForeignKey = false;
				colvarDateModified.IsReadOnly = false;
				
						colvarDateModified.DefaultSetting = @"(getdate())";
				colvarDateModified.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDateModified);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["Adex"].AddSchema("Program",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("ProgramID")]
		public int ProgramID 
		{
			get { return GetColumnValue<int>("ProgramID"); }

			set { SetColumnValue("ProgramID", value); }

		}

		  
		[XmlAttribute("ProgramCode")]
		public int ProgramCode 
		{
			get { return GetColumnValue<int>("ProgramCode"); }

			set { SetColumnValue("ProgramCode", value); }

		}

		  
		[XmlAttribute("ProgramName")]
		public string ProgramName 
		{
			get { return GetColumnValue<string>("ProgramName"); }

			set { SetColumnValue("ProgramName", value); }

		}

		  
		[XmlAttribute("MediaCode")]
		public int? MediaCode 
		{
			get { return GetColumnValue<int?>("MediaCode"); }

			set { SetColumnValue("MediaCode", value); }

		}

		  
		[XmlAttribute("Tv")]
		public bool Tv 
		{
			get { return GetColumnValue<bool>("TV"); }

			set { SetColumnValue("TV", value); }

		}

		  
		[XmlAttribute("Rd")]
		public bool Rd 
		{
			get { return GetColumnValue<bool>("RD"); }

			set { SetColumnValue("RD", value); }

		}

		  
		[XmlAttribute("Cb")]
		public bool Cb 
		{
			get { return GetColumnValue<bool>("CB"); }

			set { SetColumnValue("CB", value); }

		}

		  
		[XmlAttribute("DateAdded")]
		public DateTime DateAdded 
		{
			get { return GetColumnValue<DateTime>("DateAdded"); }

			set { SetColumnValue("DateAdded", value); }

		}

		  
		[XmlAttribute("DateModified")]
		public DateTime DateModified 
		{
			get { return GetColumnValue<DateTime>("DateModified"); }

			set { SetColumnValue("DateModified", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public Adex.CommercialLogCollection CommercialLogRecords()
		{
			return new Adex.CommercialLogCollection().Where(CommercialLog.Columns.ProgramCode, ProgramCode).Load();
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Medium ActiveRecord object related to this Program
		/// 
		/// </summary>
		public Adex.Medium Medium
		{
			get { return Adex.Medium.FetchByID(this.MediaCode); }

			set { SetColumnValue("MediaCode", value.MediaCode); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int varProgramCode,string varProgramName,int? varMediaCode,bool varTv,bool varRd,bool varCb,DateTime varDateAdded,DateTime varDateModified)
		{
			Program item = new Program();
			
			item.ProgramCode = varProgramCode;
			
			item.ProgramName = varProgramName;
			
			item.MediaCode = varMediaCode;
			
			item.Tv = varTv;
			
			item.Rd = varRd;
			
			item.Cb = varCb;
			
			item.DateAdded = varDateAdded;
			
			item.DateModified = varDateModified;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varProgramID,int varProgramCode,string varProgramName,int? varMediaCode,bool varTv,bool varRd,bool varCb,DateTime varDateAdded,DateTime varDateModified)
		{
			Program item = new Program();
			
				item.ProgramID = varProgramID;
				
				item.ProgramCode = varProgramCode;
				
				item.ProgramName = varProgramName;
				
				item.MediaCode = varMediaCode;
				
				item.Tv = varTv;
				
				item.Rd = varRd;
				
				item.Cb = varCb;
				
				item.DateAdded = varDateAdded;
				
				item.DateModified = varDateModified;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string ProgramID = @"ProgramID";
			 public static string ProgramCode = @"ProgramCode";
			 public static string ProgramName = @"ProgramName";
			 public static string MediaCode = @"MediaCode";
			 public static string Tv = @"TV";
			 public static string Rd = @"RD";
			 public static string Cb = @"CB";
			 public static string DateAdded = @"DateAdded";
			 public static string DateModified = @"DateModified";
						
		}

		#endregion
	}

}

